package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreAddLogic {
	return &StoreAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreAddLogic) StoreAdd(in *agentClient.Store) (*agentClient.IdBody, error) {
	nowTime := time.Now()
	_, err := l.svcCtx.StoreModel.Insert(l.ctx, &model.Store{
		Id:              in.StoreId,
		Platform:        in.Platform,
		Name:            in.Name,
		StoreId:         in.StoreId,
		StoreTypeId:     in.StoreTypeId,
		AgentId:         in.AgentId,
		Status:          in.Status,
		BusinessLicense: in.BusinessLicense,
		IndustryLicense: in.IndustryLicense,
		EquityPrice:     in.EquityPrice,
		StorePhone:      in.StorePhone,
		SupportTakeaway: in.SupportTakeaway,
		PackingFee:      in.PackingFee,
		CreateTime:      nowTime,
		UpdateTime:      nowTime,
		DelFlag:         in.DelFlag,
		FirstPass:       in.FirstPass,
	})
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &agentClient.IdBody{
		Id: in.StoreId,
	}, nil
}
